package org.yuanqiframework.yuanqi.boot.sys.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.web.bind.annotation.*;
import org.yuanqiframework.yuanqi.web.controller.BaseController;
import org.yuanqiframework.yuanqi.web.entity.AjaxResult;
import org.yuanqiframework.yuanqi.boot.sys.entity.SysConfig;
import org.yuanqiframework.yuanqi.boot.sys.service.SysConfigService;
import org.yuanqiframework.yuanqi.boot.sys.vo.ConfigParams;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/sys/params")
public class ParamsController extends BaseController {
    @Resource
    private SysConfigService sysConfigService;

    
    @GetMapping(value = "query")
    public AjaxResult<List<ConfigParams>> query() {
        return AjaxResult.success(sysConfigService.queryConfigParams());
    }

    
    @PostMapping(value = "sync")
    public AjaxResult<?> sync(@Valid @RequestBody ConfigParams configParams) {
        boolean update = sysConfigService.update(new LambdaUpdateWrapper<SysConfig>()
                .set(SysConfig::getSettingValue, configParams.getValue())
                .eq(SysConfig::getKeyName, configParams.getKey()));
        return toAjax(update);
    }
}